// pages/goods/detail/more-tg.js
//根据秒数计算时分秒
function computeTime(val) {
    if (val < 0) {
        return "00时00分00秒"
    }
    function format(d) {
        return d < 10 ? ("0" + d) : d
    }
    var h = parseInt(val / 3600)//时
    var _h = val - h * 3600//除去 小时 剩余的秒数
    var m = parseInt(_h / 60)//分
    var s = _h % 60//秒
    return format(h) + "时" + format(m) + "分" + format(s) + "秒"
}
let tid = 0
Page({
    data: {
        page: 1,
        total: 2,
        list: [],
    },

    onLoad: function (options) {
        options.mid = wx.getStorageSync("mid")
        this.setData(options)
    },

    onShow: function () {
        this.loadData()
    },

    loadData() {
        if (this.data.loading || this.data.page > this.data.total) {
            return
        }
        this.data.pgae == 1 && wx.showLoading({
            title: '加载中...',
        })
        this.setData({
            loading: true
        })
        getApp().http.get("group-order/list", {
            data: { id: this.data.id },
            showMsg: true,
        }).then(res => {
            res.data.items.forEach(o => {
                const ms = [{
                    name: o.member_name,
                    avatar: o.member_image
                }]
                ms.push(...o.children.map(item => {
                    return {
                        name: item.member_name,
                        avatar: item.member_image
                    }
                }))
                o.ms = ms
                o.num = +o.need_number
                // o.num = 20
            })
            let list = this.data.list
            list.push(...res.data.items)
            this.setData({
                page: this.data.page + 1,
                total: res.data.total,
                goods: res.data.goods,
                list,
                loading: false
            })
            wx.hideLoading()
            wx.stopPullDownRefresh()
            wx.setNavigationBarTitle({
                title: '团购列表 - ' + res.data.goods.name,
            })
            this.startTimer()
        }).catch(err => {
            wx.stopPullDownRefresh()
        })
    },

    startTimer() {
        clearInterval(tid)
        tid = setInterval(o => {
            for (let i = 0; i < this.data.list.length; i++) {
                let val = this.data.list[i].end_time - parseInt(+new Date() / 1000)
                console.log(val)
                if (val <= 0) {
                    continue
                }
                this.setData({
                    ['list[' + i + '].time']: computeTime(val)
                })
            }
        }, 1000)
    },

    onPullDownRefresh() {
        this.setData({
            page: 1,
            total: 2,
            list: []
        })
        this.loadData()
    },

    join(evt) {
        const { index, has } = evt.currentTarget.dataset
        if (!has) {
            wx.navigateTo({
                url: `/pages/pay/pay?id=${this.data.goods.id}&tid2=${this.data.list[index].id}`,
            })
        }
    },
})